//-----------------------------------------------------------------------
// <copyright file="Start.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2004-03-23</date>
// <summary>Starts a Win32 Service on the local computer.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.ServiceProcess
{
    using System;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Starts a Win32 Service on the local computer.
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[<ServiceProcess.Start ServiceName="serviceName" />]]></code>
    /// <para>where:</para>
    /// <para><i>serviceName (Required)</i></para>
    /// <para>The short name that identifies the service to the system.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <ServiceProcess.Start
    ///             ServiceName="W3SVC" />
    ///     </Target>
    /// </Project>
    /// ]]></code>    
    /// </example>
    [Obsolete("Please review the functionality provided by the new ControlService class")]
    public class Start : TaskBase
    {
        private string serviceName;
        private int timeout = 0;

        /// <summary>
        /// Time to wait for start to complete. 0 to not wait. Set in seconds. If timeout is non-zero and it hasn't started by then it throws an error.
        /// </summary>
        /// <value>int</value>
        public int Timeout
        {
            get
            {
                return this.timeout;
            }

            set
            {
                this.timeout = value;
            }
        }
        
        /// <summary>
        /// The short name that identifies the service to the system
        /// </summary>
        /// <value>The short name that identifies the service to the system, e.g. "W3SVC".</value>
        [Required]
        public string ServiceName
        {
            get
            {
                return (this.serviceName ?? String.Empty);
            }

            set
            {
                this.serviceName = value;
            }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            #region Execute code
            bool serviceExists = false;

            Log.LogMessageFromResources("Win32Service.Start", this.serviceName);
            System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(this.serviceName);
            try
            {
                string status = sc.Status.ToString();
                serviceExists = true;
            }
            catch (System.ComponentModel.Win32Exception)
            {
                Log.LogMessageFromResources("Win32Service.NotFound", this.serviceName);
            }

            if (serviceExists)
            {
                Log.LogMessageFromResources("Win32Service.Status", this.serviceName, sc.Status.ToString());

                if (sc.Status != System.ServiceProcess.ServiceControllerStatus.Running && sc.Status != System.ServiceProcess.ServiceControllerStatus.ContinuePending)
                {
                    sc.Start();
                    if (this.timeout != 0)
                    {
                        int counter = 0;
                        do
                        {
                            System.Threading.Thread.Sleep(1000);
                            counter++;
                            sc.Refresh();
                        }
                        while ((sc.Status != System.ServiceProcess.ServiceControllerStatus.Running) && (counter < this.timeout));
                    }

                    Log.LogMessageFromResources("Win32Service.Started", this.serviceName);
                }
            }
            #endregion
        }
    }
}
